package com.example.showImage;

import com.example.photoview.ImageModel;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.stage.Screen;
import javafx.stage.Stage;



public class ImageWindow extends Application {
    public static double widthOfWindow=1200;
    public static double heightOfWindow=800;
    private static Stage stage;
    private ImageModel imgMod;
    public ImageController imgCon;

    public ImageWindow() {
    }

    public static Stage getStage() {
        return stage;
    }

    public static double getWidthOfWindow() {
        return widthOfWindow;
    }

    public static double getHeightOfWindow() {
        return heightOfWindow;
    }

    public ImageModel getImgMod() {
        return imgMod;
    }

    public ImageController getImgCon() {
        return imgCon;
    }
    //    @Override
    public void init() throws Exception {
        super.init();
        //imgMod=null;
        //根据屏幕大小自适应设置长宽
    }


    public void start(Stage stage) {
        //获取屏幕大小
        try {
            Rectangle2D bounds = Screen.getScreens().get(0).getBounds();
            widthOfWindow = bounds.getWidth() / 1.5;
            heightOfWindow = bounds.getHeight() / 1.5;
        } catch (Exception e) {
            System.out.println("ImageWindow -- Rectangle initialize failed!");
            e.printStackTrace();
        }

        ImageWindow.stage = stage;
        System.out.println("ImageWindow -- start function started!");

        //System.out.println(getStage());
        //System.out.println(imgMod.getImageName());
        //错误

        //fLoader(URL)


//        if(Main.class.getResource("/com/example/showImage/ImageWindow.fxml")==null){
//            System.out.println("Class.Resource is null");
//           //return;
//        }

        //Parent root = null;

        //try {
        //    root = loader.load();//就算是换成初始的hello-view也不行,是controller的问题
        //}
//        catch (java.lang.IllegalStateException illegalStateException){//非法状态异常
//            //java.lang.IllegalStateException: Location is not set.
//            root = null;
//            System.out.println(illegalStateException.toString());
//        }
        //catch (IOException e) {
        //    System.out.println("ImageWindow -- start function failed!");
       //     e.printStackTrace();
       // }

        //loader.setLocation(Main.class.getResource("/showImage/fxml/ImageWindow.fxml"));
        //loader.setController();
        //System.out.println(fxmlLoader.toString());    //javafx.fxml.FXMLLoader@23a31204

        //System.out.println(fxmlLoader.getController().toString());//java.lang.IllegalStateException: Location is not set.跳过会无法显示
        FXMLLoader loader  = new FXMLLoader(getClass().getResource("/com/example/showImage/ImageWindow.fxml"));
        Scene scene =null;
        //if (root != null) {
        try {
            scene = new Scene(loader.load(), widthOfWindow, heightOfWindow);
        }catch (Exception e){
            System.out.println("Scene init failed");
            e.printStackTrace();
            //return;
        }

        //窗口属性设置

        stage.setTitle(imgMod.getImageName());

        stage.setScene(scene);

        imgCon = loader.getController();  //通过FXMLLoader获取展示窗口的controller实例

        imgCon.initImage(imgMod);//跳过前面会产生java.lang.IllegalStateException: Location is not set.会产生问题

        stage.show();
        System.out.println("A new ImageWindow summoned.");


    }

    public void setImage(ImageModel imageModel) {
        try {
            System.out.println("SetImage into start!");
            init();
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.imgMod = imageModel;
    }
}